<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TRY operations - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/inspecting.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">TRY operations</h1></div></div></div>
  

<p>In the implementation of function <code>print_half</code> we have seen the usage of the macro <a href="../../../reference/macros/tryv.html" class="api-reference"><code>BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr)</code></a>
:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">BOOST_OUTCOME_TRY</span> <span class="p">(</span><span class="k">auto</span> <span class="n">i</span><span class="p">,</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">));</span>
</code></pre></div>
<p>The <code>BOOST_OUTCOME_TRY</code> macro uses C macro overloading to select between two implementations based on the number of
input parameters. If there is exactly one input parameter i.e. without the <code>i</code>, the control statement is
roughly equivalent to:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="n">__result</span> <span class="o">=</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">__result</span><span class="p">)</span>
  <span class="k">return</span> <span class="n">__result</span><span class="p">.</span><span class="n">as_failure</span><span class="p">();</span>
</code></pre></div>
<p>Where <code>__result</code> is a compile time generated unique name. This single argument form is equivalent to
<code>BOOST_OUTCOME_TRYV(expr)</code>, incidentally.</p>

<p>If there are between two and eight parameters, this control statement is roughly equivalent to:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="n">__result</span> <span class="o">=</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">__result</span><span class="p">)</span>
  <span class="k">return</span> <span class="n">__result</span><span class="p">.</span><span class="n">as_failure</span><span class="p">();</span>
<span class="k">auto</span> <span class="n">i</span> <span class="o">=</span> <span class="n">__result</span><span class="p">.</span><span class="n">value</span><span class="p">();</span>
</code></pre></div>
<p>So here <code>i</code> as the first C macro parameter is set to the value of any successful result.</p>

<p>C macro overloads are provided for up to eight arguments. To prevent the
confounding of the C preprocessor by commas in template specifications causing more than
eight arguments appearing to the C preprocessor, you should consider wrapping the
second argument in brackets.</p>

<p>If you are within a C++ Coroutine, you ought to use <a href="../../../reference/macros/co_tryv.html" class="api-reference"><code>BOOST_OUTCOME_CO_TRYV(expr)/BOOST_OUTCOME_CO_TRY(expr)</code></a>

instead.</p>

<hr>

<h3 id="compiler-specific-extension-boost-outcome-tryx">Compiler-specific extension: <code>BOOST_OUTCOME_TRYX</code></h3>

<div class="notices note" style="background: url('../../../images/note.png') top left no-repeat padding-box padding-box;">
<div class="notices heading">note</div>
<div class="notices message"><p>This macro makes use of a proprietary extension in GCC and clang, and is not
portable. The macro is not made available on unsupported compilers,
so you can test for its presence using <code>#ifdef BOOST_OUTCOME_TRYX</code>.</p>
</div>
</div>


<p>GCC and Clang provide an extension to C++ known as
<a href="https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html" title="GCC docs on statement expressions">statement expressions</a>.
These make it possible to use a more convenient macro: <code>BOOST_OUTCOME_TRYX</code>, which is an expression. With the above macro, the above declaration of variable <code>i</code> can be rewritten to:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span> <span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">));</span>
</code></pre></div>
<p>This has an advantage that you can use it any place where you can put an expression, e.g., in &ldquo;simple initialization&rdquo;:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">if</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">)))</span>
  <span class="n">use_a_non_zero_int</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
</code></pre></div>
<p>or in as a subexpression of a bigger full expression:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="s">&#34;1&#34;</span><span class="p">))</span> <span class="o">+</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="s">&#34;2&#34;</span><span class="p">));</span>
</code></pre></div>
<p>There is also an <a href="../../../reference/macros/co_tryx.html" class="api-reference"><code>BOOST_OUTCOME_CO_TRYX(expr)</code></a>
 if you are inside a C++ Coroutine.</p>


        </div><p><small>Last revised: February 12, 2021 at 20:25:43 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/inspecting.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
